﻿//
// File:    AppView.cs
// Project: MP3 Art Embedder v0.2.x
//
// Copyright 2011-2012 Kasey Osborn (Kasewick@gmail.com)
// Ms-PL license - Use and redistribute freely
//

using System;
using System.Windows.Forms;
using Mp3LibraryViewed;
using WinController;
using WinControls;

namespace WinView
{
    static class WinProgram
    {
        [STAThread]
        static void Main (string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault (false);
            Application.Run (new AppController (args, new AppViewFactory()));
        }
    }


    // This factory allows instantiating the View from within the Controller without the
    // Controller having any visibility to the View's namespace.
    internal class AppViewFactory : IAppViewFactory
    {
        public void Make (AppController controller, Mp3LibraryModelBase modelBase)
        {
            var view = new AppView (controller, modelBase);
        }
    }


    // WinForms View component of the MVC pattern that subscribes to modelBase notifications
    // of state changes in order to keep the UI consistent with the Model.
    internal class AppView : IAppView
    {
        private AppController controller;
        private Mp3LibraryModelBase modelBase;

        public AppView (AppController controller, Mp3LibraryModelBase modelBase)
        {
            this.controller = controller;
            this.modelBase = modelBase;

            modelBase.SetView (this);

            modelBase.ActionLogged += Logger;
            modelBase.RunningChanged += ChangeBusy;
            modelBase.IsVerboseChanged += ChangeIsVerbose;
        }


        public bool Question (string message, string caption)
        {
            var answer = WinMessageBox.Show (controller.Icon, message, caption, MessageBoxButtons.OKCancel);
            return answer == DialogResult.OK || answer == DialogResult.Yes;
        }


        private void ChangeBusy (bool isBusy)
        {
            controller.rootLabel.Enabled = ! isBusy;
            controller.rootNameTextBox.Enabled = ! isBusy;
            controller.browseButton.Enabled = ! isBusy;
            controller.namesLabel.Enabled = ! isBusy;
            controller.fileNamesTextBox.Enabled = ! isBusy;
            controller.limitSizeCheckBox.Enabled = ! isBusy;
            controller.maxSizeUpDown.Enabled = ! isBusy;
            controller.maxSizeLabel.Enabled = ! isBusy;
            controller.replaceCheckBox.Enabled = ! isBusy;
            controller.whatifCheckBox.Enabled = ! isBusy;
            controller.updateButton.Enabled = ! isBusy;
            controller.cancelButton.Enabled = isBusy;

            if (! isBusy)
                controller.logBox.ScrollLastIntoView();
        }


        private void ChangeIsVerbose ()
        {
            controller.logBox.IsMultiline = modelBase.IsVerbose;
        }


        private void Logger (string message, Strength strength)
        {
            if (controller.InvokeRequired)
            {
                controller.Invoke
                (
                    new ActionLoggedEventHandler (Logger),
                    new object[] { message, strength }
                );
                return;
            }

            controller.logBox.AddMessage
            (
                message,
                strength==Strength.Normal? LogBox.LineType.Normal : LogBox.LineType.Sticky
            );
        }
    }
}
